import process from 'process'
import path from 'path'
import {getParams} from './params'
import * as step from '@flow-step/step-toolkit'
import fs from 'fs'

export function getTemplateBody(): any {
    const params = getParams()
    const stackFilePath = params.ROS_STACK_FILE_PATH
    const projectDir: string = params.projectDir
    let templateFilePath = path.join(projectDir + '/' + stackFilePath);
    if (isSuffixNotIncluded(templateFilePath)) {
        step.error(`Invalid template file extension. File: ${params.ROS_STACK_FILE_PATH} Supported: json, yaml, yml`);
        process.exit(-1)
    } else {
        let templateFileContent = fs.readFileSync(templateFilePath).toString();
        return templateFileContent
    }
}


function isSuffixNotIncluded(a: string): boolean {
    const ext = a.split('.').pop()?.toLowerCase() ?? ''; // 获取小写尾缀
    return !['json', 'yaml', 'yml'].includes(ext);
}
